const app = getApp()
const api = app.globalData.api
Page({
    data: {
        item: [],
        from: '',
        warehouseOption: [],
        warehousePopup: false,
        positionOption: [],
        positionPopup: false,
        batchOption: [],
        batchPopup: false,
        key: '',
    },
    // 获取仓库
    getWarehouse: async function (warehouseId) {
      let that = this
      let res = await api.curlPost('?cla=outStock&fun=getWarehouse', 
      {
        warehouseId: warehouseId,
        materialId: that.data.item.materialId,
        unit: that.data.item.unit
      })
      if (res.code === 1) {
          that.setData({
            warehouseOption: res.data.warehouseOption,
            ['item.warehouse']: res.data.warehouse,
            ['item.warehouseId']: res.data.warehouseId,
          })
      }
    },
    // 获取库位
    getPosition: async function (warehouseId, positionId) {
      if(warehouseId){
        let that = this
        let res = await api.curlPost('?cla=outStock&fun=getPosition', 
        {
          materialId: that.data.item.materialId, 
          unit: that.data.item.unit,
          warehouseId: warehouseId, 
          positionId: positionId
        })
        if (res.code === 1) {
          that.setData({
            positionOption: res.data.positionOption,
            ['item.position']: res.data.position,
            ['item.positionId']: res.data.positionId,
          })
        }
      }
    },
    // 获取批次
    getBatch: async function (warehouseId, positionId, batch) {
      if(warehouseId && positionId){
        let that = this
        let res = await api.curlPost('?cla=outStock&fun=getBatch', 
        {
          materialId: that.data.item.materialId,
          unit: that.data.item.unit,
          warehouseId: warehouseId,
          positionId: positionId, 
          batch: batch
        })
        if (res.code === 1) {
          that.setData({
            batchOption: res.data.batchOption,
            ['item.position']: res.data.position,
            ['item.positionId']: res.data.positionId,
            ['item.batchText']: res.data.batchText,
            ['item.batch']: res.data.batch,
          })
        }
      }
    },
    main: async function () {
      await this.getWarehouse(this.data.item.warehouseId)
      await this.getPosition(this.data.item.warehouseId, this.data.item.positionId)
      await this.getBatch(this.data.item.warehouseId, this.data.item.positionId, this.data.item.batch)
    },
    formSubmit: async function (e) {
        let data = e.detail.value,
            outStockData = wx.getStorageSync('outStockData') ? wx.getStorageSync('outStockData') : []
        if (data) {
            if(!data['warehouseId']){
              api.showToast('请选择仓库')
              return
            }
            if(!data['positionId']){
              api.showToast('请选择库位')
              return
            }
            data['money'] = (data['num']*data['price']).toFixed(app.globalData.setData.amount)
            // 初始化部分值
            if(this.data.key){
              outStockData[this.data.key] = data
            }else{
              outStockData.push(data)
            }
            api.showToast('选择成功')
            wx.setStorageSync('outStockData', outStockData)
            wx.navigateBack({
                delta: 1 //(this.data.from === 'add') ? 1 : 2
            })
        }
    },
    onStepperChange(obj) {
        this.setData({['item.num']: obj.detail})
    },
    // 关闭
    onClose: function (e) {
      let obj = e.currentTarget.dataset
      if (obj) {
          this.setData({[obj.target]: false})
      }
    },
    funRouting(type, detail) {
      switch (type) {
        case 'warehouseId':
          let warehouse = detail.value
          this.setData({
              ['item.warehouse']: warehouse['text']
          })
          return warehouse['value']
        case 'positionId':
            let position = detail.value
            this.setData({
                ['item.position']: position['text']
            })
            return position['value']
        case 'batch':
              let batch = detail.value
              this.setData({
                  ['item.batchText']: batch['text']
              })
              return batch['value']
      }
    },
    // 确认选择
    onConfirm: async function (e) {
        let obj = e.currentTarget.dataset
        let prefix = obj.prefix ? obj.prefix : 'item'
        let field = (prefix ? prefix + '.' : '') + obj.field
        let val = this.funRouting(obj.field, e.detail)
        this.setData({
            [obj.target]: false,
            [field]: val,
        })
        // 获取库位
        if(obj.field === 'warehouseId'){
          await this.getPosition(val, this.data.item.positionId)
        }else if(obj.field === 'positionId'){
          await this.getBatch(this.data.item.warehouseId, val, this.data.item.batch)
        }
    },
    showPopup: function (obj) {
      let popup = obj.currentTarget.dataset.show
      if (popup) {
          this.setData({
              [popup]: true
          });
      }
    },
    onLoad: async function (obj) {
        const from = obj.from ? obj.from : '',
            string = obj.data ? obj.data : '',
            key = obj.key ? obj.key : ''
        let array = []
        try {
            array = api.jsonToArray(api.urlDecode(string))
            array['num'] = array['num'] == 0 ? 1 : array['num']
            this.setData({
                item: array,
                from: from,
                key: key
            })
        } catch (e) {
            wx.navigateBack({delta: 1})
            api.showToast('参数错误')
        }
        await this.main()
    }
});